package com.bss.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.andy.basics.Constants;
import com.andy.basics.Service;
import com.bss.dao.IdCardTypeDao;
import com.bss.model.Passport;
import com.bss.model.Patient;
import com.bss.service.PatientService;

@Controller
@Scope("request")
public class PatientController extends com.andy.basics.Controller<Patient> {

	private static final String CLASS = AdmissionController.class.getName();

	private static final Logger DEBUG = Logger.getLogger(Constants.DEBUG_KEY + PatientController.CLASS);

	@Autowired
	private PatientService patientService;

	@Autowired
	private IdCardTypeDao idCardTypeDao;

	@Override
	public Service<Patient> getService() {
		return this.patientService;
	}

	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	@Override
	public Patient create() {
		this.patientService.setImageData(null);
		this.patientService.setImageFile(null);
		return new Patient();
	}

	@Override
	public String add() {
		// Set the image
		this.getModel().setPhoto(this.patientService.getImageData());
		return super.add();
	}

	public boolean isPassportRelated() {
		if ((this.getModel().getCountry() == null) || (this.getModel().getCountry().getId() == 0)) {
			return false;
		}
		return !"India".equalsIgnoreCase(this.getModel().getCountry().getName());
	}

	public Passport getPassport() {
		return this.patientService.getPassport();
	}

	public void selectCountry() {
		PatientController.DEBUG.debug("Country selected " + this.getModel().getCountry());
		if (this.isPassportRelated()) {
			this.getModel().setIdCardType(this.idCardTypeDao.findOnly("name='Passport'"));
		} else {
			this.getModel().setIdCardType(null);
		}
		this.getModel().setState(null);
		this.getModel().setPlace(null);
	}

	public void selectState() {
		PatientController.DEBUG.debug("State selected " + this.getModel().getState());
		this.getModel().setPlace(null);
	}

}
